package com.xxg.renrenuser.dao;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Indexes;
import com.xxg.renrenuser.dao.bean.UserBankCardInfo;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * 银行卡Dao
 *
 * @author xxg
 * @date 2020/12/5
 */
@Repository
public class UserBankCardInfoDao {
    private final MongoTemplate mongoTemplate;

    public UserBankCardInfoDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "userBankCardInfo";
        MongoCollection<Document> collection;
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            collection = mongoTemplate.createCollection(UserBankCardInfo.class);
            createIndex(collection);
        }
    }

    /**
     * 创建索引
     *
     * @param collection 数据集
     */
    private void createIndex(MongoCollection<Document> collection) {
        collection.createIndex(Indexes.descending("bindId"));
        collection.createIndex(Indexes.descending("userId"));
    }

    /**
     * 添加银行卡绑定信息
     *
     * @param record 信息
     */
    public void insertSelective(UserBankCardInfo record) {
        record.setCreateTime(new Date());
        mongoTemplate.insert(record);
    }


    /**
     * 接触绑定
     *
     * @param userId 会员ID
     * @param bindId 　绑定ID
     */
    public void deleteByUserIdBindId(String userId, String bindId) {
        Query query = new Query(where("bindId").is(bindId).and("userId").is(userId));
        mongoTemplate.remove(query, UserBankCardInfo.class);
    }

    /**
     * 获取绑定列表
     *
     * @param userId 会员ID
     * @return 绑定列表
     */
    public List<UserBankCardInfo> selectByUserId(String userId) {
        Query query = new Query(where("userId").is(userId));
        return mongoTemplate.find(query, UserBankCardInfo.class);
    }

    /**
     * 查询绑定银行卡
     *
     * @param userId 会员ID
     * @param bindId 绑定ID
     * @return 结果
     */
    public UserBankCardInfo findByBindId(String userId, String bindId) {
        Query query = new Query(where("bindId").is(bindId).and("userId").is(userId));
        return mongoTemplate.findOne(query, UserBankCardInfo.class);
    }

    /**
     * 查询是否绑定过此卡
     *
     * @param userId 会员ID
     * @param cardNo 　银行卡号
     * @return true-已绑定
     */
    public boolean findByCardNo(String userId, String cardNo) {
        Query query = new Query(where("userId").is(userId).and("cardNo").is(cardNo));
        return mongoTemplate.count(query, UserBankCardInfo.class) > 0;
    }

    public boolean hasBankcards(String userId) {
        Query query = new Query(where("userId").is(userId));
        return mongoTemplate.count(query, UserBankCardInfo.class) > 0;
    }
}